package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.DateUtils;
import com.shouhuobao.take.weight.datepicker.DatePicker;

import java.util.Date;

import droid.frame.ui.utils.Utils;

/**
 * Created by joker on 2017/6/3. <br>
 * 选择日期，分为按月选择和按日选择两种
 */
public class ParcelSelectDateActivity extends BaseActivity2 implements View.OnClickListener {

    private TextView mSelectDateTypeTV;// 选择按月选择还是按日选择
    private RelativeLayout mMonthSelectRL;// 按月选择布局
    private TextView mSelectMonthTV;// 按月选择显示选中的日期
    private View mSelectMonthV;// 选中日期下划线
    private DatePicker mSelectMonthDP;// 按月选择日期控件
    private RelativeLayout mDaySelectRL;// 按日选择布局
    private TextView mSelectStartDayTV;// 按日选择显示,开始日期选择
    private View mSelectStartDayV;
    private TextView mSelectEndDayTV;// 选择结束日期
    private View mSelectEndDayV;
    private DatePicker mSelectDayDP;// 按日选择日期控件
    private Button mSelectDateBT;

    private boolean isSelectMonth = true;// 是否按月选择,默认为按月选择
    private boolean isSelectDayStart = true;// 按日选择，更新开始日期还是结束日期，默认更新开始日期

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_selectparceldate);
        super.findViewById();
        getAppTitle().setCommonTitle("选择时间");
        mSelectDateTypeTV = (TextView) findViewById(R.id.selectdatetypeTV);
        mMonthSelectRL = (RelativeLayout) findViewById(R.id.monthselectRL);
        mSelectMonthTV = (TextView) findViewById(R.id.selectmonthTV);
        mSelectMonthV = findViewById(R.id.selectmonthV);
        mSelectMonthDP = (DatePicker) findViewById(R.id.selectmonnthDP);
        mDaySelectRL = (RelativeLayout) findViewById(R.id.dayselectRL);
        mSelectStartDayTV = (TextView) findViewById(R.id.selectstartdayTV);
        mSelectStartDayV = findViewById(R.id.selectstartdayV);
        mSelectEndDayTV = (TextView) findViewById(R.id.selectenddayTV);
        mSelectEndDayV = findViewById(R.id.selectenddayV);
        mSelectDayDP = (DatePicker) findViewById(R.id.selectdayDP);
        mSelectDateBT = (Button) findViewById(R.id.selectdateBT);

        mSelectDateTypeTV.setOnClickListener(this);
        mSelectStartDayTV.setOnClickListener(this);
        mSelectEndDayTV.setOnClickListener(this);
        mSelectDateBT.setOnClickListener(this);
        updateView(0, DateUtils.getDateYYYYMMFormat().format(new Date()), true);
        mSelectMonthDP.setTextColor(getResources().getColor(R.color.app_font)).setFlagTextColor(getResources().getColor(R.color.app_font_2)).setBackground(Color.WHITE)
                .setTextSize(Utils.dpToPx(18, getResources())).setFlagTextSize(Utils.dpToPx(18, getResources())).setRowNumber(3).setDayPickerVisible(View.GONE)
                .setOnDateChangedListener(new DatePicker.OnDateChangedListener() {

                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        String date = year + "." + (monthOfYear < 10 ? "0" + monthOfYear : monthOfYear + "");
                        updateView(0, date, true);
                    }
                });
        mSelectDayDP.setTextColor(getResources().getColor(R.color.app_font)).setFlagTextColor(getResources().getColor(R.color.app_font_2)).setBackground(Color.WHITE)
                .setTextSize(Utils.dpToPx(18, getResources())).setFlagTextSize(Utils.dpToPx(18, getResources())).setRowNumber(3).setDayPickerVisible(View.VISIBLE)
                .setOnDateChangedListener(new DatePicker.OnDateChangedListener() {

                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        if (isSelectDayStart) {
                            updateView(1, year + "." + (monthOfYear < 10 ? "0" + monthOfYear : monthOfYear + "") + "." + (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + ""), isSelectDayStart);
                        } else {
                            updateView(1, year + "." + (monthOfYear < 10 ? "0" + monthOfYear : monthOfYear + "") + "." + (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + ""), isSelectDayStart);
                        }
                    }
                });

    }

    /**
     * 根据按日选择或按月选择来更新视图，默认按月选择
     *
     * @param type    0 按月选择 1 按日选择
     * @param dateStr 日期字符串
     * @param isStart 按日选择判断是否选择的是开始日期 true选择的是开始日期,开始日期默认当前日期
     */
    private void updateView(int type, String dateStr, boolean isStart) {
        if (type == 0) {
            mSelectDateTypeTV.setText("按月选择");
            mMonthSelectRL.setVisibility(View.VISIBLE);
            mDaySelectRL.setVisibility(View.GONE);
            mSelectMonthTV.setText(dateStr);
            if (isEmpty(mSelectMonthTV.getText().toString())) {
                mSelectMonthV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
                mSelectDateBT.setBackgroundResource(R.drawable.app_parcel_button_unselect_bg);
            } else {
                mSelectMonthV.setBackgroundColor(getResources().getColor(R.color.app_font_green));
                mSelectDateBT.setBackgroundResource(R.drawable.parcel_button_select_bg);
            }
        } else {
            mSelectDateTypeTV.setText("按日选择");
            mMonthSelectRL.setVisibility(View.GONE);
            mDaySelectRL.setVisibility(View.VISIBLE);
            if (isStart) {
                mSelectStartDayTV.setText(dateStr);
                mSelectStartDayTV.setTextColor(getResources().getColor(R.color.app_font_green));
                mSelectStartDayV.setBackgroundColor(getResources().getColor(R.color.app_font_green));
                mSelectEndDayTV.setTextColor(getResources().getColor(R.color.app_font_2));
                mSelectEndDayV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
            } else {
                mSelectEndDayTV.setText(dateStr);
                mSelectEndDayTV.setTextColor(getResources().getColor(R.color.app_font_green));
                if (isEmpty(mSelectEndDayTV.getText().toString())) {
                    mSelectEndDayV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
                } else {
                    mSelectEndDayV.setBackgroundColor(getResources().getColor(R.color.app_font_green));
                }
                mSelectStartDayTV.setTextColor(getResources().getColor(R.color.app_font_2));
                mSelectStartDayV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
            }
            if (isEmpty(mSelectStartDayTV.getText().toString()) || isEmpty(mSelectEndDayTV.getText().toString())) {
                mSelectDateBT.setBackgroundResource(R.drawable.app_parcel_button_unselect_bg);
            } else {
                mSelectDateBT.setBackgroundResource(R.drawable.parcel_button_select_bg);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 点击选择按日选择还是按月选择
            case R.id.selectdatetypeTV:
                // 判断是按月选择还是按日选择
                if (isSelectMonth) {// 选择按日选择
                    isSelectMonth = false;
                    // 判断选择开始日期还是结束日期,true选择开始日期
                    if (isSelectDayStart) {
                        if (isEmpty(mSelectStartDayTV.getText().toString())) {
                            updateView(1, DateUtils.getDateFormat().format(new Date()), isSelectDayStart);
                        } else {
                            updateView(1, mSelectStartDayTV.getText().toString(), isSelectDayStart);
                        }
                    } else {// 选择结束日期
                        updateView(1, mSelectEndDayTV.getText().toString(), isSelectDayStart);
                    }
                } else {
                    isSelectMonth = true;
                    updateView(0, mSelectMonthTV.getText().toString(), true);
                }
                break;
            // 按日选择开始日期
            case R.id.selectstartdayTV:
                try {
                    isSelectDayStart = true;
                    mSelectDayDP.setDate(DateUtils.getDateFormat().parse(mSelectStartDayTV.getText().toString()));
                    updateView(1, mSelectStartDayTV.getText().toString(), isSelectDayStart);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            // 按日选择结束日期
            case R.id.selectenddayTV:
                try {
                    isSelectDayStart = false;
                    if (!isEmpty(mSelectEndDayTV.getText().toString())) {
                        mSelectDayDP.setDate(DateUtils.getDateFormat().parse(mSelectEndDayTV.getText().toString()));
                    }
                    updateView(1, mSelectEndDayTV.getText().toString(), isSelectDayStart);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            // 选择确定时间按钮
            case R.id.selectdateBT:
                try {
                    // 按月选择返回
                    if (isSelectMonth) {
                        if (isEmpty(mSelectMonthTV.getText().toString())) {
                            showToast("请选择日期");
                        } else {
                            Intent intent = new Intent();
                            intent.putExtra("type", 0);
                            intent.putExtra("date", mSelectMonthTV.getText().toString());
                            setResult(Activity.RESULT_OK, intent);
                            this.finish();
                        }
                    } else {
                        if (isEmpty(mSelectEndDayTV.getText().toString())) {
                            showToast("请选择结束日期");
                        } else {
                            Intent intent = new Intent();
                            intent.putExtra("type", 1);
                            // 开始日期大于结束日期 0 开始日期大于结束日期 1 开始日期等于结束日期 2 开始日期小于结束日期
                            if (DateUtils.getDateFormat().parse(mSelectStartDayTV.getText().toString()).getTime() > DateUtils.getDateFormat().parse(mSelectEndDayTV.getText().toString()).getTime()) {
                                intent.putExtra("datetype", 0);
                                intent.putExtra("startdate", mSelectEndDayTV.getText().toString());
                                intent.putExtra("enddate", mSelectStartDayTV.getText().toString());
                            } else if (DateUtils.getDateFormat().parse(mSelectStartDayTV.getText().toString()).getTime() == DateUtils.getDateFormat().parse(mSelectEndDayTV.getText().toString())
                                    .getTime()) {// 开始日期等于结束日期
                                intent.putExtra("datetype", 1);
                                intent.putExtra("startdate", mSelectStartDayTV.getText().toString());
                                intent.putExtra("enddate", mSelectEndDayTV.getText().toString());
                            } else {// 开始日期小于结束日期
                                intent.putExtra("datetype", 2);
                                intent.putExtra("startdate", mSelectStartDayTV.getText().toString());
                                intent.putExtra("enddate", mSelectEndDayTV.getText().toString());
                            }
                            setResult(Activity.RESULT_OK, intent);
                            this.finish();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
